﻿using DTO;
using IBll;
using IDal;
using Model;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Bll
{
    public class SroreBll: ISroreBll
    {
        public SroreBll(IsroreDal<Srore> dal, IsroreDal<Area> dall)
        {
            Dal = dal;
            Dall = dall;
        }

        public IsroreDal<Srore> Dal { get; }
        public IsroreDal<Area> Dall { get; }


        /// <summary>
        /// 新增
        /// </summary>
        /// <param name="srore">门店表</param>
        /// <returns>返回受影响行数</returns>
        public async Task<int> AddSrore(Srore srore)
        {
            bool user = Dal.GetAllAsync().Any(x=>x.SroreName.Equals(srore.SroreName));
            if (user!=null)
            {
                return await Dal.AddAsync(srore);
            }
            return -1;
        }

        /// <summary>
        /// 显示+分页+查询
        /// </summary>
        /// <param name="pageindex">当前页</param>
        /// <param name="pagesize">每页显示条数</param>
        /// <param name="name">门店名称</param>
        /// <param name="time">创建日期</param>
        /// <returns></returns>

        public SroreDto ShowSrore(int pageindex, int pagesize, string? name, DateTime? time)
        {
            var strore = Dal.GetAllAsync();
            var area=Dall.GetAllAsync();
            var list = from a in strore
                       join b in area
                       on a.SAreaId equals b.AreaId
                       select new
                       {
                           a.SroreRoomName,
                           a.SroreNumber,
                           a.SrorePhone,
                           a.SrorePage,
                           a.SroreName,
                           a.SroreId,
                           a.SAreaId,
                           a.SroreTime,
                           b.AreaName

                       };
            if (name!=null)
            {
                list = list.Where(x=>x.SroreName.Equals(name));
            }
            if (time!=null)
            {
                var dl= time.Value.AddDays(1);
                list = list.Where(x=>x.SroreTime>= time&&x.SroreTime<= dl);

            }
            var c=list.Count();
            var d=list.OrderBy(x=>x.SroreId).Skip((pageindex-1) *pagesize).Take(pagesize);

            return new SroreDto { Count = c, Srore=d };
        }

        /// <summary>
        /// 删除
        /// 逻辑
        /// </summary>
        /// <param name="id">主键</param>
        /// <returns>根据主键返回门店表</returns>
        public async Task<int> IDelete(int id)
        {
           var m=Dal.GetAllAsync().FirstOrDefault(x=>x.SroreId.Equals(id));
            m.DeleteData=DateTime.Now;
            m.IsDeleted=true;
            return await Dal.UpdateAsync(m);

        }

        /// <summary>
        /// 反填
        /// </summary>
        /// <param name="id">主键</param>
        /// <returns>根据主键进行反填</returns>
        public Srore Find(int id)
        {
            return Dal.GetAllAsync().FirstOrDefault(x=>x.SroreId.Equals(id));
        }

        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="srore">门店表</param>
        /// <returns>返回受影响行数</returns>
        public async Task<int> Update(Srore srore)
        {
            bool user = Dal.GetAllAsync().Any(x=>x.SroreName.Equals(srore.SroreName)&&x.SroreId.Equals(srore.SroreId));
            if (user!=null)
            {
                srore.UpdateData = DateTime.Now;
                return await Dal.UpdateAsync(srore);

            }
            return -1;
        }







    }
}
